package com.example.administrator.runbook.fragments.community.utils;

import com.example.administrator.runbook.fragments.community.model.Comment;
import com.example.administrator.runbook.fragments.community.model.Tweets;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/8.
 */

public class ParserData {

    private static final ParserData parserData = new ParserData();

    public static ParserData getInstance() {
        return parserData;
    }

    public List<Tweets> getHot(JSONObject jsonObject) {
        List<Tweets> list = null;
        if (jsonObject != null) {
            boolean ok = jsonObject.optBoolean("ok");
            if (ok) {
                JSONArray array = jsonObject.optJSONArray("tweets");
                int length = array.length();
                if (length > 0) {
                    list = new ArrayList<>();
                    for (int i = 0; i < length; i++) {
                        Tweets tweets = new Tweets();
                        JSONObject object = array.optJSONObject(i);
                        JSONObject user = object.optJSONObject("user");
                        String uid = user.optString("_id");
                        tweets.set_id(uid);
                        String avatar = user.optString("avatar");
                        tweets.setAvatar(avatar);
                        String nickname = user.optString("nickname");
                        tweets.setNickname(nickname);
                        String type = user.optString("type");
                        tweets.setType(type);
                        String lv = user.optString("lv");
                        tweets.setLv(lv);
                        String gender = user.optString("gender");
                        tweets.setGender(gender);

                        JSONObject tweet = object.optJSONObject("tweet");
                        String title = tweet.optString("title");
                        tweets.setTitle(title);
                        String content = tweet.optString("content");
                        tweets.setContent(content);
                        String commented = tweet.optString("commented");
                        tweets.setCommented(commented);
                        String retweeted = tweet.optString("retweeted");
                        tweets.setRetweeted(retweeted);
                        String date = tweet.optString("created");
                        tweets.setCreated(date);
                        String id = tweet.optString("_id");
                        tweets.setId(id);
                        list.add(tweets);
                    }
                }
            }
        }
        return list;
    }

    public List<Comment> getComment(JSONObject jsonObject) {
        List<Comment> list = null;
        if (jsonObject != null) {
            boolean ok = jsonObject.optBoolean("ok");
            if (ok) {
                JSONArray array = jsonObject.optJSONArray("comments");
                int length = array.length();
                if (length > 0) {
                    list = new ArrayList<>();
                    for (int i = 0; i < length; i++) {
                        Comment comment = new Comment();
                        JSONObject object = array.optJSONObject(i);
                        String id = object.optString("_id");
                        comment.set_id(id);
                        String tweet = object.optString("tweet");
                        comment.setTweet(tweet);
                        String content = object.optString("content");
                        comment.setContent(content);
                        String floor = object.optString("floor");
                        comment.setFloor(floor);
                        String tweetAuthor = object.optString("tweetAuthor");
                        comment.setTweetAuthor(tweetAuthor);
                        String created = object.optString("created");
                        comment.setCreated(created);

                        JSONObject author = object.optJSONObject("author");
                        String uid = author.optString("_id");
                        comment.setUid(uid);
                        String avatar = author.optString("avatar");
                        comment.setAvatar(avatar);
                        String nickname = author.optString("nickname");
                        comment.setNickname(nickname);
                        String lv = author.optString("lv");
                        comment.setLv(lv);
                        String gender = author.optString("gender");
                        comment.setGender(gender);

                        JSONObject replyTo = object.optJSONObject("replyTo");
                        if (replyTo != null) {
                            String reid = replyTo.optString("_id");
                            comment.setReId(reid);
                            String refloor = replyTo.optString("floor");
                            comment.setRefloor(refloor);

                            JSONObject reAuthor = replyTo.optJSONObject("author");
                            String reuid = reAuthor.optString("_id");
                            comment.setReuid(reuid);
                            String reNickName = reAuthor.optString("nickname");
                            comment.setRenickname(reNickName);
                        }
                        list.add(comment);
                    }
                }
            }
        }
        return list;
    }

    public Tweets getHotDetial(JSONObject object) {
        Tweets tweets = new Tweets();
        if (object != null) {
            boolean ok = object.optBoolean("ok");
            if (ok) {
                JSONObject user = object.optJSONObject("user");
                String uid = user.optString("_id");
                tweets.set_id(uid);
                String avatar = user.optString("avatar");
                tweets.setAvatar(avatar);
                String nickname = user.optString("nickname");
                tweets.setNickname(nickname);
                String type = user.optString("type");
                tweets.setType(type);
                String lv = user.optString("lv");
                tweets.setLv(lv);
                String gender = user.optString("gender");
                tweets.setGender(gender);

                JSONObject tweet = object.optJSONObject("tweet");
                String title = tweet.optString("title");
                tweets.setTitle(title);
                String content = tweet.optString("content");
                tweets.setContent(content);
                String commented = tweet.optString("commented");
                tweets.setCommented(commented);
                String retweeted = tweet.optString("retweeted");
                tweets.setRetweeted(retweeted);
                String date = tweet.optString("created");
                tweets.setCreated(date);
                String id = tweet.optString("_id");
                tweets.setId(id);

            }
        }
        return tweets;
    }
}
